package com.clouds.wms.cms;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.clouds.wms.bean.Cmsmd;
import com.clouds.wms.bean.Cmsme;
import com.clouds.wms.bean.Cmsmk;
import com.clouds.wms.exception.SysException;
import com.clouds.wms.service.CmsmbService;
import com.clouds.wms.service.CmsmdService;
import com.clouds.wms.service.CmsmeService;
import com.clouds.wms.service.CmsmkService;
import com.clouds.wms.service.CmspdaService;
import com.clouds.wms.util.Function;
import com.clouds.wms.util.MD5;
import com.clouds.wms.util.PagesBean;
import com.clouds.wms.util.RequestHelper;

@Controller
@RequestMapping("/cmsmd")
public class CmsmdController {

	private Log log = LogFactory.getLog(getClass());
	@Resource(name="cmsmbService")
	private CmsmbService cmsmbService;
	@Resource(name="cmsmdService")
	private CmsmdService cmsmdService;
	@Resource(name="cmsmeService")
	private CmsmeService cmsmeService;
	@Resource(name="cmsmkService")
	private CmsmkService cmsmkService;
	@Resource(name="cmspdaService")
	private CmspdaService cmspdaService;
	/**
	 * 登录
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws SysException
	 * @throws IOException 
	 */
	@RequestMapping(value = "/login.xhtml")
	public String login(HttpServletRequest request, HttpServletResponse response, Model model) throws SysException, IOException {
		String mdaa = request.getParameter("mdaa");
		mdaa = new String(mdaa.getBytes("ISO-8859-1"), "UTF-8");
		response.setContentType("text/xml;charset=utf-8");
		response.setHeader("Pragma", "No-cache");
		response.setDateHeader("Expires", 0);
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		if(mdaa == null || "".equals(mdaa)) {
			out.print("员工编号不能为空!");
			return null;
		}
		String mdab = request.getParameter("mdab");
		if(mdab == null || "".equals(mdab)) {
			out.print("密码不能为空!");
			return null;
		}
		Cmsmd cmsmd = cmsmdService.getCmsmdByMdaa(mdaa);
		if(cmsmd == null) {
			out.print("员工编号错误!");
			return null;
		}
		if(!MD5.encoderForString(mdab).equals(cmsmd.getMdab())) {
			out.print("密码错误!");
			return null;
		}
		request.getSession().setAttribute("globalUmUser", cmsmd);
		return null;
	}
	
	/**
	 * 退出
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws SysException
	 */
	@RequestMapping(value = "/logout.xhtml")
	public String logout(HttpServletRequest request, HttpServletResponse response, Model model) throws SysException {
		request.getSession().removeAttribute("globalUmUser");
		return "redirect:/index.jsp";
	}

	/**
	 * 首页
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws SysException
	 */
	@RequestMapping(value = "/main.xhtml")
	public String main(HttpServletRequest request, HttpServletResponse response, Model model) throws SysException {
		return "cms/main";
	}

	/**
	 * 底部
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws SysException
	 */
	@RequestMapping(value = "/footer.xhtml")
	public String footer(HttpServletRequest request, HttpServletResponse response, Model model) throws SysException {
		return "cms/footer";
	}

	/**
	 * 左侧
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws SysException
	 */
	@RequestMapping(value = "/left.xhtml")
	public String left(HttpServletRequest request, HttpServletResponse response, Model model) throws SysException {
		Cmsmd globalUmUser = (Cmsmd)request.getSession().getAttribute("globalUmUser");
		List<Cmsme> cmsmeList = cmsmeService.getCmsmeListByMbaa(globalUmUser.getMdad());
		
		Map<Long, List<Cmsme>> cmsmeMap = new HashMap<Long, List<Cmsme>>();
		List<Cmsme> cmsmeList1 = new ArrayList<Cmsme>();
		
		for(Cmsme cmsme : cmsmeList) {
			if(cmsme.getMeac() == 0) {
				cmsmeList1.add(cmsme);
			} else {
				List<Cmsme> temp = cmsmeMap.get(cmsme.getMeac());
				if(temp == null) {
					temp = new ArrayList<Cmsme>();
				}
				temp.add(cmsme);
				cmsmeMap.put(cmsme.getMeac(), temp);
			}
		}
		model.addAttribute("cmsmeList", cmsmeList1);
		model.addAttribute("cmsmeMap", cmsmeMap);
		return "cms/left";
	}
	/**
	 * 员工管理
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws SysException
	 */
	@RequestMapping(value = "/cmsmdMain.xhtml")
	public String cmsmdMain(HttpServletRequest request, HttpServletResponse response, Model model) throws SysException {
		model.addAttribute("allCmsmbList", cmsmbService.getAllCmsmbList());
		model.addAttribute("allCmspdaList", cmspdaService.getAllCmspdaList());
		return "cms/cmsmd";
	}
	


	/**
	 * 列出所有的员工
	 * @param request
	 * @param response
	 * @return
	 * @throws SysException
	 */
	
	@RequestMapping(value = "/cmsmdList.xhtml")
	public String cmsmdList(HttpServletRequest request, HttpServletResponse response, Model model) throws SysException {
		String mdaa = RequestHelper.getString(request, "mdaa");
		String mdac = RequestHelper.getString(request, "mdac");
		String mdad = RequestHelper.getString(request, "mdad");
		// 分页参数-start
		int pageNumber = RequestHelper.getInt(request, "page", 1);// 要跳转的页号
		int pageSize = RequestHelper.getInt(request, "rows", 10);// 一页的条数
		PagesBean pb = cmsmdService.getCmsmdList(mdaa, mdac, mdad, pageNumber, pageSize);
		List<Cmsmd> list = ((List<Cmsmd>)pb.getData());
		int count = pb.getCount();
		Map<String,Object> paraMap = new HashMap<String,Object>();
		paraMap.put("total", count);
		paraMap.put("rows", list);
		try {
			Function.toJson(response, paraMap);
		} catch (IOException e) {
			log.error(e.getMessage());
		}
		return null;
	}

	/**
	 * 添加员工
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws SysException
	 * @throws IOException 
	 */
	@RequestMapping(value = "/saveCmsmd.xhtml", method = RequestMethod.POST)
	public String saveCmsmd(HttpServletRequest request, HttpServletResponse response, Model model, Cmsmd cmsmd) throws SysException, IOException {
		String mdaa = cmsmd.getMdaa();
		Cmsmd globalUmUser = (Cmsmd)request.getSession().getAttribute("globalUmUser");
		response.setContentType("text/xml;charset=utf-8");
		response.setHeader("Pragma", "No-cache");
		response.setDateHeader("Expires", 0);
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		if(mdaa == null || "".equals(mdaa.trim())) {
			out.print("员工编号不能为空!");
			return null;
		}
		Cmsmd cmsmd1 = cmsmdService.getCmsmdByMdaa(mdaa);
		if(cmsmd1 != null && cmsmd1.getMdaa() != null && !"".equals(cmsmd1.getMdaa())) {
			out.print("员工编号已经存在!");
			return null;
		}
		cmsmd.setMdab(MD5.encoderForString(cmsmd.getMdab()));
		//保存信息
		cmsmdService.saveCmsmd(cmsmd);
		Cmsmk cmsmk = new Cmsmk();
		cmsmk.setMkab(String.valueOf(globalUmUser.getMdaa()));
		cmsmk.setMkac(globalUmUser.getMdac());
		cmsmk.setMkad("增加了编号为[" + mdaa + "]的员工!");
		cmsmk.setMkae(new Date());
		cmsmk.setMkaf(request.getRemoteAddr());
		cmsmkService.saveCmsmk(cmsmk);
		out.print("success");
		return null;
	}

	/**
	 * 修改员工
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws SysException
	 * @throws IOException 
	 */
	@RequestMapping(value = "/updateCmsmd.xhtml", method = RequestMethod.POST)
	public String updateCmsmd(HttpServletRequest request, HttpServletResponse response, Model model, Cmsmd cmsmd) throws SysException, IOException {
		Cmsmd globalUmUser = (Cmsmd)request.getSession().getAttribute("globalUmUser");
		response.setContentType("text/xml;charset=utf-8");
		response.setHeader("Pragma", "No-cache");
		response.setDateHeader("Expires", 0);
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		//保存信息
		cmsmdService.updateCmsmd(cmsmd);
		Cmsmk cmsmk = new Cmsmk();
		cmsmk.setMkab(String.valueOf(globalUmUser.getMdaa()));
		cmsmk.setMkac(globalUmUser.getMdac());
		cmsmk.setMkad("修改了编号为[" + cmsmd.getMdaa() + "]的员工!");
		cmsmk.setMkae(new Date());
		cmsmk.setMkaf(request.getRemoteAddr());
		cmsmkService.saveCmsmk(cmsmk);
		out.print("success");
		return null;
	}

	/**
	 * 删除员工
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws SysException
	 * @throws IOException 
	 */
	@RequestMapping(value = "/deleteCmsmd.xhtml", method = RequestMethod.POST)
	public String deleteCmsmd(HttpServletRequest request, HttpServletResponse response, Model model) throws SysException, IOException {
		String mdaa = RequestHelper.getString(request, "mdaa");
		response.setContentType("text/xml;charset=utf-8");
		response.setHeader("Pragma", "No-cache");
		response.setDateHeader("Expires", 0);
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		cmsmdService.deleteCmsmdByMdaa(mdaa);
		Cmsmd globalUmUser = (Cmsmd)request.getSession().getAttribute("globalUmUser");
		Cmsmk cmsmk = new Cmsmk();
		cmsmk.setMkab(String.valueOf(globalUmUser.getMdaa()));
		cmsmk.setMkac(globalUmUser.getMdac());
		cmsmk.setMkad("删除了编号为[" + mdaa + "]的员工!");
		cmsmk.setMkae(new Date());
		cmsmk.setMkaf(request.getRemoteAddr());
		cmsmkService.saveCmsmk(cmsmk);
		out.print("success");
		return null;
	}

	/**
	 * 批量删除员工
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws SysException
	 * @throws IOException 
	 */
	@RequestMapping(value = "/deleteCmsmdBatch.xhtml", method = RequestMethod.POST)
	public String deleteCmsmdBatch(HttpServletRequest request, HttpServletResponse response, Model model) throws SysException, IOException {
		String mdaas = RequestHelper.getString(request, "mdaa");
		String[] mdaaStrs = mdaas.split(",");
		for(String mdaa : mdaaStrs) {
			cmsmdService.deleteCmsmdByMdaa(mdaa);
			Cmsmd globalUmUser = (Cmsmd)request.getSession().getAttribute("globalUmUser");
			Cmsmk cmsmk = new Cmsmk();
			cmsmk.setMkab(String.valueOf(globalUmUser.getMdaa()));
			cmsmk.setMkac(globalUmUser.getMdac());
			cmsmk.setMkad("删除了编号为[" + mdaa + "]的员工!");
			cmsmk.setMkae(new Date());
			cmsmk.setMkaf(request.getRemoteAddr());
			cmsmkService.saveCmsmk(cmsmk);
		}
		response.setContentType("text/xml;charset=utf-8");
		response.setHeader("Pragma", "No-cache");
		response.setDateHeader("Expires", 0);
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		out.print("success");
		return null;
	}

	/**
	 * 修改密码
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws SysException
	 */
	@RequestMapping(value = "/passwordMain.xhtml")
	public String passwordMain(HttpServletRequest request, HttpServletResponse response, Model model) throws SysException {
		return "cms/password";
	}

	/**
	 * 修改密码
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws SysException
	 * @throws IOException 
	 */
	@RequestMapping(value = "/updatePassword.xhtml")
	public String updatePassword(HttpServletRequest request, HttpServletResponse response, Model model) throws SysException, IOException {
		String oldpassword = RequestHelper.getString(request, "oldpassword");
		String password = RequestHelper.getString(request, "password");
		String repassword = RequestHelper.getString(request, "repassword");
		Cmsmd globalUmUser = (Cmsmd)request.getSession().getAttribute("globalUmUser");
		response.setContentType("text/xml;charset=utf-8");
		response.setHeader("Pragma", "No-cache");
		response.setDateHeader("Expires", 0);
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		Cmsmd cmsmd = cmsmdService.getCmsmdByMdaa(globalUmUser.getMdaa());
		if(cmsmd == null) {
			out.print("请重新登录!");
			return null;
		}
		if(!cmsmd.getMdac().equals(MD5.encoderForString(oldpassword))) {
			out.print("原始密码不正确!");
			return null;
		}
		if(password == null || repassword == null || !password.trim().equals(repassword.trim())) {
			out.print("重复密码不正确!");
			return null;
		}
		cmsmdService.updateCmsmdMdab(cmsmd.getMdaa(), MD5.encoderForString(password));
		out.print("success");
		return null;
	}
	
}
